package heap;

import java.util.PriorityQueue;

public class leetcode215数组中的第K个最大元素 {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (queue.size() == k) {
                if (nums[i] > queue.peek()) {
                    queue.poll();
                    queue.offer(nums[i]);
                }
            } else {
                queue.offer(nums[i]);
            }
        }
        return queue.peek();
    }
}
